"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 证书文件格式不正确,并将日志输出到指定的文件
Create At   : 2021/10/14
Owner       : @qumin20145
Description :
    1..将需要替换的证书zip包放在相应的路径下：/xxx/fafinde_normal.zip
    2.替换证书将日志输出到指定的文件：
    gs_om -t cert --cert-file=/xxx/fafinde_normal.zip -l /xxx/cert.log
    3.清理环境
Expect      :
    1.证书的zip包放置成功
    2.证书替换失败,日志输出成功
    3.清理环境
History     : 
"""
import os
import subprocess
import unittest

from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.log = Logger()
        self.com = Common()
        self.log.info('----Opengauss_Function_Tools_gs_om_Case0114_start----')
        self.log.info(vars(self.pri_user))
        self.f_name_1 = 'normal.zip'
        self.f_name_2 = 'error.zip'
        self.cur_path_1 = os.path.join(macro.SCRIPTS_PATH, self.f_name_1)
        self.cur_path_2 = os.path.join(macro.SCRIPTS_PATH, self.f_name_2)
        self.target_path = macro.DB_BACKUP_PATH
        self.certlog_path = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH))

    def test_tools_om(self):
        text = '----step1.1: 判断当前主机是否数据库主机 expect: 成功----'
        self.log.info(text)
        cmd = "ifconfig -a|grep inet6 -a2|grep broadcast|awk '{print $2}'"
        self.log.info(cmd)
        self.cur_ip = subprocess.getoutput(cmd).strip()
        self.log.info(self.cur_ip)

        text = '----step1.2: 复制当前主机的文件到数据库主机 expect: 成功----'
        self.log.info(text)
        if self.cur_ip == self.pri_user.ssh_host:
            self.log.info('当前主机是数据库主机，无需远程复制')
            cmd = f"cp {self.cur_path_1} {self.target_path};" \
                f"cp {self.cur_path_2} {self.target_path}"
            self.log.info(cmd)
            res = self.pri_user.sh(cmd).result()
            self.log.info(res)
        else:
            self.com.scp_file(self.pri_user, self.f_name_1, self.target_path)
            self.com.scp_file(self.pri_user, self.f_name_2, self.target_path)

        text = '----step1.3: 查看数据库主机目标路径下文件 expect: 成功----'
        self.log.info(text)
        cmd = f"ls -l {self.target_path}|" \
            f"grep -E '{self.f_name_1}|{self.f_name_2}'"
        self.log.info(cmd)
        res = self.pri_user.sh(cmd).result()
        self.log.info(res)
        text = '-----step2:替换证书将日志输出到指定的文件;expect:证书替换失败,日志输出成功------'
        self.log.info(text)
        cert_cmd = f'source {macro.DB_ENV_PATH};' \
        f'gs_om -t cert ' \
        f'--cert-file' \
        f'={os.path.join(macro.DB_BACKUP_PATH,"error.zip")} ' \
        f'-l {self.certlog_path}/cert.log ;'
        self.log.info(cert_cmd)
        cert_msg = self.pri_user.sh(cert_cmd).result()
        self.log.info(cert_msg)
        self.assertIn('Missing SSL client cert file in ZIP file', cert_msg,
                      '执行失败' + text)

        self.log.info('------校验日志是否输出成功-----')
        find_cmd = f'find {self.certlog_path} -name \'cert*\' -mmin -1; '
        self.log.info(find_cmd)
        find_msg = self.pri_user.sh(find_cmd).result()
        self.log.info(find_msg)
        self.assertIn('cert', find_msg, '执行失败' + text)

    def tearDown(self):
        text = '------step5.1:清理环境;except:清理成功-----'
        self.log.info(text)
        clear_cmd = f'rm -rf {self.certlog_path}/cert*;'
        self.log.info(clear_cmd)
        clear_msg = self.pri_root.sh(clear_cmd).result()
        self.log.info(clear_msg)
        text = '----step5.2: 清理数据库主机目标路径下文件 expect: 成功----'
        self.log.info(text)
        if self.cur_ip == self.pri_user.ssh_host:
            cmd = f"rm -rf {os.path.join(self.target_path, self.f_name_1)} " \
                f"{os.path.join(self.target_path, self.f_name_2)}"
            self.log.info(cmd)
            res = self.pri_user.sh(cmd).result()
            self.log.info(res)
        else:
            self.com.scp_file(self.pri_user, self.f_name_1, self.target_path,
                              del_sign=True)
            self.com.scp_file(self.pri_user, self.f_name_2, self.target_path,
                              del_sign=True)
        text = '----step5.3: 查看清理后数据库主机目标路径下文件 expect: 成功----'
        self.log.info(text)
        cmd = f"ls -l {self.target_path}|grep {self.f_name_1};" \
            f"ls -l {self.target_path}|grep {self.f_name_2}"
        self.log.info(cmd)
        res = self.pri_user.sh(cmd).result()
        self.log.info(res)
        self.assertEqual('', clear_msg, '执行失败:' + text)
        self.assertEqual('', res, '执行失败:' + text)
        self.log.info(
            '------Opengauss_Function_Tools_gs_om_Case0114_finish-----')
